package com.module.recall.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;

import base.dao.GenericHibernateDao;

import com.module.codeManage.setcode.vo.FirstCode;
import com.module.recall.commom.SetCodeConstant;
import com.module.recall.vo.TagCode;

public class FirstCodeDao extends GenericHibernateDao<FirstCode, Integer>
		implements SetCodeConstant {
	static FirstCodeDao dao;
	public SecondCodeDao secondCodeDao = SecondCodeDao.getInstance();
	private Log log = LogFactory.getLog(FirstCode.class);

	public FirstCodeDao() {

	}

	public synchronized static FirstCodeDao getInstance() {
		if (dao == null) {
			dao = new FirstCodeDao();
		}
		return dao;
	}

	/**
	 * 
	 * @param id
	 * @return
	 */
	public FirstCode selectByPk(Session session, int id) throws Exception {
		FirstCode fc = null;
		Query query = session.createQuery(" from  FirstCode as a where a.id="
				+ id);
		List list = query.list();
		if (list != null) {
			fc = (FirstCode) list.get(0);
		}
		return fc;
	}

	public FirstCode selectInfoByCode(Session session, String code)
			throws Exception {
		FirstCode fc = new FirstCode();
		Query query = session
				.createQuery(" from  FirstCode as a where a.codeInfo='" + code
						+ "'");
		List list = query.list();
		if (list != null && !list.isEmpty()) {
			fc = (FirstCode) list.get(0);
		}
		return fc;
	}

	// 查询码是否存在
	public boolean selectByCode(Session session, String code) throws Exception {
		boolean flag = false;
		Query query = session
				.createQuery(" select count(*) from FirstCode  as a where a.codeInfo='"
						+ code + "'");
		long n = ((Long) query.uniqueResult()).longValue();
		if (n == 0) {
			flag = false;// 不存在
		} else if (n == 1) {
			flag = true; // 存在
		}
		return flag;
	}

	/**
	 * 查出码相应的信息，如码对应组织，卷号，位置。。。
	 * 
	 * @param codeInfo
	 * @param session
	 * @return
	 * @throws SQLException
	 * @throws Exception
	 */
	public TagCode selectCodeInfo(Session session, String code)
			throws SQLException, Exception {
		TagCode tagCode = new TagCode();
		boolean flag = secondCodeDao.selectByCode(session, code);
		if (flag) {
			// 在二层码中查询到数据
			tagCode.setTypeNum(2);
			// 获取第一层的ID
			int fristId = secondCodeDao.selectFirstIdByCode(session, code);
			FirstCode fc = selectByPk(session, fristId);
			if (fc != null) {
				tagCode.setFirstCodeInfo(fc.getCodeInfo());
				tagCode.setSecondCodeInfo(code);
				tagCode.setOrgId(fc.getOrgId());
				tagCode.setState(fc.getState());
				tagCode.setType(fc.getType());
				tagCode.setRollNum(fc.getRollNum());
				tagCode.setSequenceNum(fc.getSequenceNum());
			}
		} else {
			// 在二层码中没有查询到数据,跑到一层码中去查
			tagCode.setTypeNum(1);
			boolean f = selectByCode(session, code);
			if (f) {
				// 在一层码库中找到数据,根据type的值去判断是否需要查双层码表
				FirstCode fcd = selectInfoByCode(session, code);
				int type = fcd.getType();// 1=单层 2=双层
				if (type == 1) {
					tagCode.setFirstCodeInfo(code);
					tagCode.setOrgId(fcd.getOrgId());
					tagCode.setState(fcd.getState());
					tagCode.setType(fcd.getType());
					tagCode.setRollNum(fcd.getRollNum());
					tagCode.setSequenceNum(fcd.getSequenceNum());
				} else {
					tagCode.setFirstCodeInfo(code);
					tagCode.setOrgId(fcd.getOrgId());
					tagCode.setState(fcd.getState());
					tagCode.setType(fcd.getType());
					tagCode.setRollNum(fcd.getRollNum());
					tagCode.setSequenceNum(fcd.getSequenceNum());
					String secondCode = secondCodeDao.slectCodeInfoByFirstId(
							session, fcd.getId());
					tagCode.setSecondCodeInfo(secondCode);
				}
			} else {
				// 在一层码库中都没找到数据
				log.info("在数据库中没有找到传入的码号");
			}
		}
		return tagCode;
	}
}
